package com.yswee.mallnavi.view.brand;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.utils.UnitUtils;
import com.yswee.mallnavi.R;


public class LetterView extends View {

	protected OnTouchingLetterChangedListener onTouchingLetterChangedListener;

	protected String[] b;

	protected int fontColor = 0;
	protected int selColor = 0;
	protected int fontSize = 13;
	protected int choose = -1;

	protected Paint paint = new Paint();
	protected Paint paintBg = new Paint();
	protected boolean showBkg = false;

	public LetterView(Context context) {
		this(context, null);
	}

	public LetterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initStyle(attrs);
		b = StringUtils.getGroupKeys();
	}

	@SuppressWarnings("deprecation")
	protected void initStyle(AttributeSet attrs) {
		TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
				R.styleable.LetterListView);
		
		fontColor = getResources().getColor(R.color.widget_letter_fg);
		selColor = getResources().getColor(R.color.widget_letter_sel_fg);
		fontSize = (int) getResources().getDimension(R.dimen.widget_letter_fontsize);
		fontColor = typedArray.getColor(R.styleable.LetterListView_LetterListView_fontColor, fontColor);
		selColor = typedArray.getColor(R.styleable.LetterListView_LetterListView_selColor, selColor);
		fontSize = (int) typedArray.getDimension(R.styleable.LetterListView_LetterListView_fontSize, fontSize);

		paintBg.setColor(getContext().getResources().getColor(R.color.widget_letter_sel_bg));

		typedArray.recycle();
	}

	protected void onSuperDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

	@SuppressWarnings("deprecation")
	protected void onDraw(Canvas canvas) {
		onSuperDraw(canvas);
		if (showBkg) {
			canvas.drawColor(getResources().getColor(R.color.widget_letter_bg));
		}

		int height = getHeight() - UnitUtils.dip2pix(getContext(), 6);
		int width = getWidth();
		int singleHeight = height / b.length;
		for (int i = 0; i < b.length; i++) {
			paint.setTextSize(fontSize);
			paint.setColor(fontColor);
			//paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setAntiAlias(true);
			if (i == choose) {
				paint.setColor(selColor);
				paint.setFakeBoldText(true);
			}
			float xPos = width / 2 - paint.measureText(b[i]) / 2;
			float yPos = singleHeight * i + singleHeight;

			if (i == choose) {
				float size = getContext().getResources().getDimension(R.dimen.dp_15);
				float centerY = yPos - fontSize / 2;
				RectF rectF = new RectF((width - size) / 2,
						centerY - size / 2,
						width - (width - size) / 2,
						centerY + size / 2);
				canvas.drawOval(rectF, paintBg);
			}

			canvas.drawText(b[i], xPos, yPos, paint);

			paint.reset();
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final float x = event.getX();
		final int oldChoose = choose;
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		final int c = (int) (y / getHeight() * b.length);

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			showBkg = true;
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c], y, x);
					choose = c;
					invalidate();
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c], y, x);
					choose = c;
					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			showBkg = false;
			choose = -1;
			invalidate();
			break;
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	public interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(String s, float y, float x);
	}
}